package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.dao.EquipDAO;
import com.model._do.EquipmentDO;
import com.model._do.SignUpDO;
import com.model._do.UserDO;
import com.model.dto.baseDTO.ResultDTO;
import com.service.EquipmentService;
import com.service.ResultService;
import com.service.SignUpService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/map")
public class MapController {

    @Autowired
    private UserService userService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private ResultService resultService;

    @Autowired
    private SignUpService signUpService;

    /**
     * 根据province，city筛选同城医生
     * @param equipment
     * @return
     */
    @RequestMapping(value = "/getNearDoctor", method = RequestMethod.POST)
    public ResultDTO getNearDoctor(@RequestBody EquipmentDO equipment){
        JSONObject jsonObject = JSONObject.parseObject(equipment.getEquipUserLocation());
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("province", jsonObject.getString("province"));
        hashMap.put("city", jsonObject.getString("city"));
        return resultService.createSuccess("success", userService.getNearDoctor(hashMap));
    }

    /**
     * 根据province，city筛选同城设备
     * @param doctor
     * @return
     */
    @RequestMapping(value = "/getNearEquip", method = RequestMethod.POST)
    public ResultDTO getNearEquipment(@RequestBody UserDO doctor){
        JSONObject jsonObject = JSONObject.parseObject(doctor.getUserLocation());
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("province", jsonObject.getString("province"));
        hashMap.put("city", jsonObject.getString("city"));
        List<EquipmentDO> equipList = equipmentService.getNearEquip(hashMap);

        SignUpDO sign = new SignUpDO();
        sign.setUserId(doctor.getUserId());
        sign.setAsUserType("DOCTOR");
        List<SignUpDO> signList = signUpService.getSignedByUserId(sign);

        HashMap<String, Object> hasMap = new HashMap<>();
        signList.forEach(s-> hasMap.put(s.getEquipId(), new Object()));

        List<Object> objList = equipList.stream().map(e -> hasMap.get(e.getEquipId()) == null? e: null).filter(Objects::nonNull).collect(Collectors.toList());
        return resultService.createSuccess("success", objList);
    }
}
